<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<title>我的订单</title>
	<meta name="keywords" content="KEYWORDS..." />
	<meta name="description" content="DESCRIPTION..." />
	<meta name="author" content="DeathGhost" />
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name='apple-touch-fullscreen' content='yes'>
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="format-detection" content="telephone=no">
	<meta name="format-detection" content="address=no">
	<meta name="viewport" content="initial-scale=1, width=device-width, maximum-scale=1, user-scalable=no">
	<link rel="stylesheet" type="text/css" href="__STATIC__/home/css/reset.css">
	<link rel="stylesheet" type="text/css" href="__STATIC__/home/css/style.css" />
	<link rel="stylesheet" type="text/css" href="__STATIC__/home/layui/css/layui.css" />
	<meta name="viewport"
		content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<link rel="stylesheet" type="text/css" href="__STATIC__/home/css/swiper3.07.min.css" />
	<style>
		.tab {
			top: 0;
		}

		.my-order-iphone1 {
			margin-top: 4%;
		}

		.addclasstruenav {
			color: #3acd17;
		}

		.padd_40 {
			padding-top: 33px;
		}

		.food_div {
			width: 95%;
			background-color: #fff;
			height: 150px;
			border-radius: 15px;
			margin: 0 auto;
			margin-top: 12px;
			position: relative;
		}

		.layui-form-checkbox[lay-skin=primary] i {
			top: 60px;
			left: 10px;
		}

		.food_div img {
			width: 80px;
			height: 80px;
			margin-left: 10px;
			margin-top: 30px;
			position: absolute;
		}

		.num_div {
			position: absolute;
			right: 20px;
			bottom: 40px;
			border: 1px solid #999;
			border-radius: 5px;
			width: 70px;
		}

		.num_div span {
			width: 28%;
			text-align: center;
			display: inline-block;
		}

		.food_num {
			border-right: 0.5px solid #999;
			border-left: 0.5px solid #999;
		}

		.sumPrice {
			width: 100px;
			display: inline-block;
			float: right;
			height: 100%;
			line-height: 36px;
			margin-right: 10px;
		}

		.Sum {
			color: red;
			float: right;
		}

		.bottom_div {
			margin-bottom: 0;
			position: fixed;
			bottom: 50px;
			background-color: #fff;
			height:auto;
			width:100%;
			padding:10px 0;
		}
	</style>
</head>

<body {if condition="$is_have eq 0" }style="background-color:#fff;" {/if}>
	{if condition ="$is_have eq 0"}
	<p style="text-align: center;width: 100%;font-size: 0.4rem;color:#333;margin-top: 6rem;">当前购物车暂无餐品，快去购买吧！</p>
	{else/}
	<form class="layui-form" action="" style="margin-bottom: 130px;">
		<input type="hidden" id="user_id" value="{$Think.session.user_id}">
		<p style="padding-left:9.75px;color:#666;padding-top:10px;">共{$count}件宝贝</p>
		{volist name="temp_arr" id="temp"}
		<div class="food_div">
			<input type="checkbox" food_id="{$temp.food_id}" lay-skin="primary">
			<img src="{$temp.img}">
			<p style="width:65%;margin-top:25px;float: right;">{$temp.name}</p>
			<p style="width:65%;float:right;margin-top:45px;color:red">￥<span class="onePrice">{$temp.price}</span></p>
			<div class="num_div" food_id="{$temp.food_id}">
				<span class="dec">-</span>
				<span class="food_num">{$temp.num}</span>
				<span class="inc">+</span>
			</div>
		</div>
		{/volist}
		<div class="layui-form-item bottom_div">
			<div class="layui-input-block" style="margin-left:20px;">
				<button class="layui-btn createOrder" type="button"
					style="border-radius: 30px;padding:0px 20px;float: right;margin-right: 10px;">结算</button>
				<p class="sumPrice">合计：<span style="color:red;">￥</span><span class="Sum">0</span></p>
				<button class="layui-btn layui-btn-danger delCartFood" type="button"
					style="border-radius: 30px;padding:0px 20px;float: left;margin-right: 10px;">删除</button>
			</div>
		</div>
	</form>
	{/if}

	<script src="__STATIC__/home/layui/layui.js" type="text/javascript" charset="utf-8"></script>
	<script>
		layui.use('form', function () {
			var form = layui.form;
			var temp = 0;
			form.on('checkbox()', function (data) {
				var countPrice = 0;
				var arr = [];
				$(".layui-form-checked").each(function () {
					var food_id = $(this).prev().attr("food_id");
					var onePrice = $(this).parent().find(".onePrice").text();
					var num = $(this).parent().find(".food_num").text();
					temp = parseInt(onePrice) * parseInt(num);
					arr.push(temp);
				});
				for (var i = 0; i < arr.length; i++) {
					countPrice = arr[i] + countPrice;
				}
				$(".Sum").text(countPrice);
			});
		});
	</script>

	{include file="../application/home/view/common/footer.html"}

	<script src="__STATIC__/home/js/jquery.js" type="text/javascript" charset="utf-8"></script>
	<script src="__STATIC__/home/js/swiper.jquery.min.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		var user_id = $("#user_id").val();
		$(".dec").click(function () {
			var food_id = $(this).parent().attr("food_id");
			var temp_num = $(this).next().text();
			if (temp_num == 1) { return; }
			temp_num = temp_num - 1;
			$(this).next().text(temp_num);
			var data = {};
			data.food_id = food_id;
			data.num = temp_num;
			$.ajax({
				type: 'post',
				url: "/home/cart/changeCart",
				data: { data: data },
				dataType: 'json',
				async: false
			});

			if ($(this).parent().parent().find(".layui-form-checkbox").hasClass("layui-form-checked")) {
				var countPrice = 0;
				var arr = [];
				$(".layui-form-checked").each(function () {
					var food_id = $(this).prev().attr("food_id");
					var onePrice = $(this).parent().find(".onePrice").text();
					var num = $(this).parent().find(".food_num").text();
					temp = parseInt(onePrice) * parseInt(num);
					arr.push(temp);
				});
				for (var i = 0; i < arr.length; i++) {
					countPrice = arr[i] + countPrice;
				}
				$(".Sum").text(countPrice);
			}
		})

		$(".inc").click(function () {
			var food_id = $(this).parent().attr("food_id");
			var temp_num = $(this).prev().text();
			temp_num = parseInt(temp_num) + 1;
			$(this).prev().text(temp_num);
			var data = {};
			data.food_id = food_id;
			data.num = temp_num;
			$.ajax({
				type: 'post',
				url: "/home/cart/changeCart",
				data: { data: data },
				dataType: 'json',
				async: false
			});

			if ($(this).parent().parent().find(".layui-form-checkbox").hasClass("layui-form-checked")) {
				var countPrice = 0;
				var arr = [];
				$(".layui-form-checked").each(function () {
					var food_id = $(this).prev().attr("food_id");
					var onePrice = $(this).parent().find(".onePrice").text();
					var num = $(this).parent().find(".food_num").text();
					temp = parseInt(onePrice) * parseInt(num);
					arr.push(temp);
				});
				for (var i = 0; i < arr.length; i++) {
					countPrice = arr[i] + countPrice;
				}
				$(".Sum").text(countPrice);
			}
		})

		$(".createOrder").click(function () {
			if ($(".Sum").text() == 0) { return; }
			var countPrice = 0;
			var data = {};
			var arr = [];
			$(".layui-form-checked").each(function () {
				var food_id = $(this).prev().attr("food_id");
				var onePrice = $(this).parent().find(".onePrice").text();
				var num = $(this).parent().find(".food_num").text();
				temp = parseInt(onePrice) * parseInt(num);
				data[food_id] = num;
				arr.push(temp);
			});
			for (var i = 0; i < arr.length; i++) {
				countPrice = arr[i] + countPrice;
			}
			var jsonStr = JSON.stringify(data);
			// console.log(jsonStr);
			// return;
			$.ajax({
				type: 'post',
				url: "/home/order/createOrder",
				data: { jsonStr: jsonStr, sum: countPrice },
				dataType: 'json',
				async: false,
				success: function (data) {
					console.log(data.order_id);
					if (data.status) {
						layer.msg("成功创建订单", {
							time: 2000,
							icon: 6
						});
						setTimeout(function () {
							window.location.href = '/home/order/setorder?order_id=' + data.order_id;
						}, 2000);
					}
				}
			});
		});

		$(".delCartFood").click(function () {
			layer.confirm('是否要删除当前选中的餐品?', { icon: 3, title: '提示' }, function (index) {
				var arr = [];
				$(".layui-form-checked").each(function () {
					var food_id = $(this).prev().attr("food_id");
					arr.push(food_id);
				});
				$.ajax({
					type: 'post',
					url: "/home/cart/delCartFood",
					data: { arr: arr },
					dataType: 'json',
					async: false,
					success: function (data) {
						if (data.status) {
							layer.msg("删除成功", {
								time: 2000,
								icon: 6
							});
							setTimeout('window.location.href="/home/cart/cart"', 2000);
						}
					}
				});
				layer.close(index);
			});
		});
	</script>
</body>

</html>